package badugabot;
/**
 * @author Wojtek N., Kamil M.
 * @version 0.7
 * Class which extends PlayerInfo and add some methods especially for bot.
 */
import badugaclient.PlayerInfo;

public class BotInfo extends PlayerInfo
{
	/**
	 * Method which selects cards to exchange.
	 */
	public void setBotCardChange() 
	{
		String[] keepIt = botComparison(getHand());
		for(int i=0; i<4; i++)
		{
			//System.out.println(getHand().substring(2*i, 2*i+2)+" "+keepIt[i]);
			if(getHand().substring(2*i, 2*i+2).equals(keepIt[i]))
			{
				setCardsToChange("F", i);
			}
			else
			{
				setCardsToChange("T", i);
			}
		}
	}
	/**
	 * Method in which bot separates cards for exchanging from ones it wants to keep. 
	 * @param hand full hand of the bot
	 * @return table of cards which bot wants to keep
	 */
	public String[] botComparison(String hand)
	{
		String[] temp = new String[4];
		String[] ranks = new  String[4];
		String[] suits = new String[4];
		for(int i=0; i<4; i++)
		{
			ranks[i] = hand.substring(i*2, i*2+1);
			suits[i] = hand.substring(i*2+1, i*2+2);
			temp[i] = "";
		}
		final String[] allRanks = {"A","2","3","4","5","6","7","8","9","T","J","Q","K"};
		int[] allRanksPlace = new int[4];
		for(int i=0; i<allRanksPlace.length; i++)
		{
			for(int j=0; j<allRanks.length; j++)
			{
				if(ranks[i].equals(allRanks[j]))
				{
					allRanksPlace[i] = j;
				}
			}
			//System.out.println("Card: "+ranks[i]+suits[i]+" ARP: "+allRanksPlace[i]);
		}
		
		// First drop all cards higher than 9.
		for(int i=0; i<ranks.length; i++)
		{
			if(allRanksPlace[i] > 8)
			{
				ranks[i] = "0";
				suits[i] = "0";
			}
		}
		
		// Than check for the same suits.
		for(int i=0; i<ranks.length; i++)
		{
			// if card have same suit with some other card then
			if(suits[i%4].equals(suits[(i+1)%4]) || 
					suits[i%4].equals(suits[(i+2)%4]) || 
					suits[i%4].equals(suits[(i+3)%4]))
			{
				// if card have same rank with some other card then
				if(ranks[i%4].equals(ranks[(i+1)%4]) || 
						ranks[i%4].equals(ranks[(i+2)%4]) || 
						ranks[i%4].equals(ranks[(i+3)%4]))
				{
					// if your rank is bigger from rank of card with which you are compared than go out
					if((allRanksPlace[i%4] >= allRanksPlace[(i+1)%4] && suits[i%4].equals(suits[(i+1)%4])) ||
							(allRanksPlace[i%4] >= allRanksPlace[(i+2)%4] && suits[i%4].equals(suits[(i+2)%4])) ||
							(allRanksPlace[i%4] >= allRanksPlace[(i+3)%4] && suits[i%4].equals(suits[(i+3)%4])))
					{
						ranks[i] = "0";
						suits[i] = "0";
					}
				}
				// if card have only same suit with some other card then
				else
				{
					// if your rank is bigger from rank of card with which you are compared than go out
					if((allRanksPlace[i%4] >= allRanksPlace[(i+1)%4] && suits[i%4].equals(suits[(i+1)%4])) ||
							(allRanksPlace[i%4] >= allRanksPlace[(i+2)%4] && suits[i%4].equals(suits[(i+2)%4])) ||
							(allRanksPlace[i%4] >= allRanksPlace[(i+3)%4] && suits[i%4].equals(suits[(i+3)%4]))) 
					{
						ranks[i] = "0";
						suits[i] = "0";
					}
				}
			}
		}
		
		// Finally check for same ranks.
		for(int i=0; i<ranks.length; i++)
		{	
			// if card have same rank with some other card then
			if(ranks[i%4].equals(ranks[(i+1)%4]) || 
				ranks[i%4].equals(ranks[(i+2)%4]) || 
				ranks[i%4].equals(ranks[(i+3)%4]))
			{
				// if your rank is bigger from rank of card with which you are compared than go out
				if((allRanksPlace[i%4] >= allRanksPlace[(i+1)%4] && ranks[i%4].equals(ranks[(i+1)%4])) ||
						(allRanksPlace[i%4] >= allRanksPlace[(i+2)%4] && ranks[i%4].equals(ranks[(i+2)%4])) ||
						(allRanksPlace[i%4] >= allRanksPlace[(i+3)%4] && ranks[i%4].equals(ranks[(i+3)%4])))
				{
					ranks[i] = "0";
					suits[i] = "0";
				}
			}
		}
		
		for(int i=0; i<ranks.length; i++)
		{
			temp[i] += ranks[i];
			temp[i] += suits[i];
		}
		return temp;
	}
}
